/*
 * Created by JFormDesigner on Thu Jun 27 15:13:36 CST 2024
 */

package scct.yzx.ui;

import scct.yzx.dao.UserDao;
import scct.yzx.model.User;
import scct.yzx.utils.Tools;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author lenovo
 */
public class DlgUserAdd extends JDialog {
    public DlgUserAdd(Window owner) {
        super(owner);
        initComponents();
    }




    private void btnAdd(ActionEvent e) {
        // TODO add your code here


        String username = txtName.getText();
        String password = txtPass.getText();
        String createtime = Tools.getCreatetime();

//        Long createtime = System.currentTimeMillis();
////      使用date里的方法将获取到的时间11位数字转换问年月日形式
//        Date date = new Date(createtime);// 将11位强转成时间
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 转换位年月日形式
//        String str = sdf.format(date);// 获取
        if (!username.matches("^\\w{3,8}$")){
            JOptionPane.showMessageDialog(this,"请输入合法的用户名");
            return;
        }if (!password.matches("^\\w{3,8}$")) {
            JOptionPane.showMessageDialog(this, "请输入合法的密码");
            return;
        }

//        重复命名
        UserDao userDao = new UserDao();
        if (userDao.isDupname(username)){
            JOptionPane.showMessageDialog(this,"用户名重复，请重新添加");
            return;
        }
        if (userDao.addUser(username,password,createtime)){
            JOptionPane.showMessageDialog(this,"添加成功");

        }
        this.dispose();
    }

    private void btnCancle(ActionEvent e) {
        // TODO add your code here
        this.dispose();
    }



    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - yzx
        label1 = new JLabel();
        txtName = new JTextField();
        label2 = new JLabel();
        txtPass = new JTextField();
        btnAdd = new JButton();
        btnCancle = new JButton();

        //======== this ========
        setTitle("\u6dfb\u52a0\u7528\u6237");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u7528\u6237\u540d");
        contentPane.add(label1);
        label1.setBounds(new Rectangle(new Point(65, 35), label1.getPreferredSize()));
        contentPane.add(txtName);
        txtName.setBounds(135, 31, 80, 25);

        //---- label2 ----
        label2.setText("\u5bc6\u7801");
        contentPane.add(label2);
        label2.setBounds(new Rectangle(new Point(65, 80), label2.getPreferredSize()));
        contentPane.add(txtPass);
        txtPass.setBounds(135, 75, 80, 25);

        //---- btnAdd ----
        btnAdd.setText("\u6dfb\u52a0");
        btnAdd.addActionListener(e -> btnAdd(e));
        contentPane.add(btnAdd);
        btnAdd.setBounds(new Rectangle(new Point(70, 175), btnAdd.getPreferredSize()));

        //---- btnCancle ----
        btnCancle.setText("\u53d6\u6d88");
        btnCancle.addActionListener(e -> btnCancle(e));
        contentPane.add(btnCancle);
        btnCancle.setBounds(new Rectangle(new Point(185, 175), btnCancle.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - yzx
    private JLabel label1;
    private JTextField txtName;
    private JLabel label2;
    private JTextField txtPass;
    private JButton btnAdd;
    private JButton btnCancle;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
